package com.mine.util;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Arrays;

public class FormatReport {

	static boolean flag = false;

	public static ArrayList<String[]> formatJCKApiReport(String path)
			throws Exception {
		int i = 0;
		ArrayList<String[]> al = new ArrayList<String[]>();
		BufferedReader br = InputReport.inputFile(path);
		String[] result = new String[3];
		while (true) {
			String line = br.readLine();
			if (i == 2)
				break;
			if (line == null) {
				br.close();
				break;
			}
			if (line.contains("<testclass classname=")) {
				String classname;
				int biginIndex = line.indexOf("'") + 1;
				classname = line.substring(biginIndex);
				int endIndex = classname.indexOf("'");
				classname = classname.substring(0, endIndex);
				result[0] = classname;
			}
			if (line.contains("STATUS:")) {
				flag = true;
				if (line.contains("all passed")) {
					result[1] = "PASS";
				} else if (line.contains("first test case failure:")) {
					result[1] = "FAIL";
				} else {
					result[1] = "ERROR";
				}
			}
			if (line.contains("<time>")) {
				if (!flag) {
					result[1] = "ERROR";
				}
				String time;
				int beginIndex = line.indexOf(">") + 1;
				time = line.substring(beginIndex);
				int endIndex = time.indexOf("<");
				time = time.substring(0, endIndex);
				result[2] = time;
				al.add(result);
				result = new String[3];
				flag = false;
			}
		}
		return al;
	}

	public static ArrayList<String[]> formatJCKLangReport(String path)
			throws Exception {
		ArrayList<String[]> al = new ArrayList<String[]>();
		BufferedReader br = InputReport.inputFile(path);
		String[] result = new String[3];
		while (true) {
			String line = br.readLine();
			if (line == null) {
				br.close();
				break;
			}
			if (line.contains("<testclass classname=")) {
				String classname;
				int biginIndex = line.indexOf("'") + 1;
				classname = line.substring(biginIndex);
				int endIndex = classname.indexOf("'");
				classname = classname.substring(0, endIndex);
				result[0] = classname;
			}
			if (line.contains("rxCICxqyx 95")) {
				flag = true;
				result[1] = "PASS";
			} else if (line.contains("9YzwHqgmhA 97")) {
				flag = true;
				result[1] = "FAIL";
			}

			if (line.contains("<time>")) {
				if (!flag) {
					result[1] = "ERROR";
				}
				String time;
				int beginIndex = line.indexOf(">") + 1;
				time = line.substring(beginIndex);
				int endIndex = time.indexOf("<");
				time = time.substring(0, endIndex);
				result[2] = time;
				al.add(result);
				result = new String[3];
				flag = false;
			}
		}
		return al;
	}

	public static ArrayList<String[]> formatStressReport(String path)
			throws Exception {
		// TODO
		ArrayList<String[]> al = new ArrayList<String[]>();
		BufferedReader br = InputReport.inputFile(path);
		String[] result = new String[3];
		while (true) {
			String line = br.readLine();
			if (line == null) {
				br.close();
				break;
			}
			if (line.contains("<property-item name=\"TestID\">")) {
				String classname;
				int biginIndex = line.indexOf("org");
				classname = line.substring(biginIndex);
				int endIndex = classname.indexOf("]]");
				classname = classname.substring(0, endIndex);
				result[0] = classname;
			}
			if (line.contains("<property-item name=\"exectime\">")) {
				String time;
				int beginIndex = line.indexOf(":") + 1;
				time = line.substring(beginIndex);
				int endIndex = time.indexOf("parts");
				time = time.substring(0, endIndex);
				result[2] = time;
			}
			if (line.contains("<property-item name=\"Status\">")) {
				if(line.contains("FAILED")){
					result[1] = "FAILED";
				}else if(line.contains("PASSED")){
					result[1] = "PASS";
				}else if(line.contains("ERROR")){
					result[1] = "ERROR";
				}
				System.out.println(result[1]);
				al.add(result);
				result = new String[3];
			}
		}
		return al;
	}

	public static ArrayList<String[]> formatReport(String path, String type)
			throws Exception {
		ArrayList<String[]> al = null;
		
		if (("Harmony").equals(type)) {
			//TODO:
		}else if(("JCK_API").equals(type)) {
			al = formatJCKApiReport(path);
		} else if (("JCK_LANG").equals(type)) {
			al = formatJCKLangReport(path);
		} else if (("JNI").equals(type)) {
			//TODO:
		} else if(("STRESS").equals(type)){
			al = formatStressReport(path);
		}
		return al;
	}

	public static void main(String[] args) throws Exception {
		FormatReport
				.formatStressReport("F:\\Stress\\K-Touch\\dalvik\\KTouch_DALVIKVM_NOJIT�汾_150s\\org\\apache\\harmony\\test\\stress\\api\\java\\io\\ObjectOutputStream\\writeObjectReadObject0026\\writeObjectReadObject0026.thr");
	}
}
